﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=397704
// To debug code on page load in Ripple or on Android devices/emulators: launch your app, set breakpoints, 
// and then run "window.location.reload()" in the JavaScript Console.
(function () {
  "use strict";

  document.addEventListener('deviceready', onDeviceReady.bind(this), false);

  function onDeviceReady() {
    // Handle the Cordova pause and resume events
    document.addEventListener('pause', onPause.bind(this), false);
    document.addEventListener('resume', onResume.bind(this), false);

    document.addEventListener("backbutton", onBackKeyDown, false);

    // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
    //alert("device ready");

    // Hide splashscreen
    //navigator.splashscreen.hide();


    var admobid = (/(android)/i.test(navigator.userAgent)) ? "ca-app-pub-4419218839484727/3655631691" : "ca-app-pub-4419218839484727/6013668890";


    // Set AdMobAds options:
    admob.setOptions({
      publisherId: admobid
    });

    // Start showing banners (atomatic when autoShowBanner is set to true)
    admob.createBannerView();

    // Request interstitial (will present automatically when autoShowInterstitial is set to true)
    admob.requestInterstitial();





  };

  function onPause() {
    // TODO: This application has been suspended. Save application state here.
    //window.removeEventListener('shake', shakeEventDidOccur, false);
    myShakeEvent.stop();
    myShakeEvent = null;
      //alert("PAUSE");
  };

  function onResume() {
    // TODO: This application has been reactivated. Restore application state here.
    myShakeEvent = new Shake({
      threshold: 13
      //,timeout: 1000 
    });
    myShakeEvent.start();
    window.addEventListener('shake', shakeEventDidOccur, false);

     //alert("RESUME");
  };


  function onBackKeyDown(event) {
      //alert("EXITING APP....  STOP SOUND.....");
    //window.removeEventListener('shake', shakeEventDidOccur, false);


    //if (myShakeEvent != null) {
    //  alert("its nothing");
    //}
    //else {
    //  alert("its something");
    //}
    myShakeEvent.stop();
    myShakeEvent = null;
    navigator.app.exitApp();

      //navigator.app.exitApp();
  }

})();

var myShakeEvent = null;

window.onload = function () {
  //alert("window load");
  //create a new instance of shake.js.
   myShakeEvent = new Shake({
    threshold: 13
    //,timeout: 1000 
  });

  // start listening to device motion
  myShakeEvent.start();

  // register a shake event
  window.addEventListener('shake', shakeEventDidOccur, false);

  $('#imgGlove').click(function () {
    shakeEventDidOccur();
  });
  


  //shake event callback
  function shakeEventDidOccur() {
   // alert("SHAKE");
    //put your own code here etc.
    //alert('Shake!');    fadeout(400, func    fadein(400)
    //$("#imgGlove").fadeOut(400);

    //$('#imgSlap').fadeIn();
    //$('#pic').hide();

   // $('#imgGlove').hide();
    //$("#imgSlap").show();
    $('#imgGlove').hide();
    $('#imgSlap').show();
    //$('#imgGlove').fadeIn(200);

    //var url = "/android_asset/www/slap.mp3";
    var url = (/(android)/i.test(navigator.userAgent)) ? "/android_asset/www/slap.mp3" : "slap.mp3";


    var my_media = new Media(url,
            // success callback
             function () { console.log("playAudio():Audio Success"); },
            // error callback
             function (err) { console.log("playAudio():Audio Error: " + err); }
    );
    // Play audio
    my_media.play();

    //alert("djkfdajfklda");
    //$('#imgGlove').fadeOut(200);
    //$('#imgGlove').attr("src", "glove.svg");
   // $('#imgSlap').hide();
   // $('#imgGlove').fadeIn();

    setTimeout(function () {
      $('#imgSlap').hide();
      $('#imgGlove').show();

    }, 400);

    
    //$('#pic').hide();
    //$('#imgSlap').fadeOut();
    //$("#imgGlove").fadeIn(200);

  }
};



